package com.iwinding.ladygg.ui.act;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iwinding.ladygg.AppForegroundStateManager;
import com.iwinding.ladygg.ICaneApplication;
import com.iwinding.ladygg.R;


/**
 * Created by Administrator on 2016/6/7.
 */
public abstract class BaseFragmentActivity extends AppCompatActivity implements SwipeRefreshLayout.OnRefreshListener {

    protected ICaneApplication mOAAplication;
    protected ImageView mBack;
    protected TextView mBackTxt;
    protected TextView mTitle;
    protected ImageView mRight;
    protected TextView mRightTxt;
    protected LinearLayout mContent;
    protected LinearLayout mHead;
//    protected SwipeRefreshLayout swipeLayout;


    protected ProgressDialog mProgress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        if (this instanceof LoginActivity)
//            WindowUtil.fullScreenStatusBar(this, false);
//        else {
//            WindowUtil.bepaintStatusBar(this, getResources().getColor(R.color.colorBlue));
//        }

        setContentView(R.layout.icane_activity_base);
        mOAAplication = (ICaneApplication) getApplication();

        mHead = findViewById(R.id.base_head);
        mContent = findViewById(R.id.base_content);
        mBack = findViewById(R.id.base_head_back);
        mRight = findViewById(R.id.base_head_right);
        mTitle = findViewById(R.id.base_head_title);
        mBackTxt = findViewById(R.id.base_back_txt);
        mRightTxt = findViewById(R.id.base_right_txt);
//        mHead = (LinearLayout) findViewById(R.id.base_head);
//        mContent = (LinearLayout) findViewById(R.id.base_content);
//        mBack = (ImageView) findViewById(R.id.base_head_back);
//        mRight = (ImageView) findViewById(R.id.base_head_right);
//        mTitle = (TextView) findViewById(R.id.base_head_title);
//        mBackTxt = (TextView) findViewById(R.id.base_back_txt);
//        mRightTxt = (TextView) findViewById(R.id.base_right_txt);
//        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
//        swipeLayout.setOnRefreshListener(this);
//        swipeLayout.setColorSchemeColors(android.R.color.holo_blue_bright,
//                android.R.color.holo_green_light,
//                android.R.color.holo_orange_light,
//                android.R.color.holo_red_light);

        if (mBack != null) {
            mBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }

        int layouid = getLayoutID();
        View content = View.inflate(this, layouid, null);
        mContent.removeAllViews();
        content.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        mContent.addView(content);
    }

    protected abstract int getLayoutID();


//    protected void setBackFunction(){
//
//    }

    protected void setHeadVisibility(int visibility) {
        mHead.setVisibility(visibility);
    }

    protected void setTitleStr(String title) {
        if (mTitle != null) {
            mTitle.setText(title);
        }
    }


    protected void setTitleResId(int resString) {
        if (mTitle != null) {
            mTitle.setText(getString(resString));
        }
    }

    protected void setBackTxtResId(int resString) {
        if (mBackTxt != null) {
            mBackTxt.setText(getString(resString));
            mBackTxt.setVisibility(View.VISIBLE);
        }
    }

    protected void setRightTxtResId(int resString) {
        if (mRightTxt != null) {
            mRightTxt.setText(getString(resString));
            mRightTxt.setVisibility(View.VISIBLE);
        }
    }

    protected void setmRightIcon(int resIcon) {
        if (mRight != null) {
            mRight.setImageResource(resIcon);
            mRight.setVisibility(View.VISIBLE);
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mRight != null)
            mRight.setVisibility(mBack.getVisibility() != View.GONE ? View.INVISIBLE : View.GONE);
        AppForegroundStateManager.getInstance().onActivityVisible(this);
    }

    @Override
    protected void onStop() {
        super.onStop();
        AppForegroundStateManager.getInstance().onActivityNotVisible(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    public void loadingProgressDialog() {
        if (mProgress != null) {
            mProgress.dismiss();
            mProgress = null;
        }
        mProgress = new ProgressDialog(this);
        mProgress.show();
    }

//    public void loadingLoginDialog() {
//        if (mProgress != null) {
//            mProgress.dismiss();
//            mProgress = null;
//        }
//        mProgress = new ProgressDialog(this, ProgressDialog.Type.GIF);
//        mProgress.show();
//    }

    public void dismissProgressDialog() {
        mProgress.dismiss();
    }


    @Override
    public void onRefresh() {
//        new Handler().postDelayed(new Runnable() {
//            @Override public void run() {
//                swipeLayout.setRefreshing(false);
//            }
//        }, 5000);
    }
}
